package com.lj.constroller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lj.entity.Emp;
import com.lj.service.EmpService;
import com.lj.service.EmpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class EmController {
    @Autowired
    EmpServiceImpl empService;
    @RequestMapping("/emps")
    public String ShowEmps(@RequestParam(value="page",defaultValue="1")Integer page,Model model)
    {
        System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"+page);
        Page<Object> objects = PageHelper.startPage(page, 4);
        List<Emp> emps = empService.QueryAllEmp();
        model.addAttribute("emps",emps);
        return "emp/list";
    }
    @RequestMapping("/templates/emp")
    public String addShowEmps(Model model)
    {
        List<Emp> emps = empService.QueryAllEmp();
        model.addAttribute("depts",emps);
        return "emp/add";
    }
    @PostMapping("/emp")
    public String addEmps(Emp emp)
    {
        System.out.println(emp);
        int Count = empService.AddEmp(emp);
        return "redirect:/emps";
    }
//    修改页面
    @RequestMapping( value = "/emp/{id}")
    public String toEditPage(@PathVariable("id") Integer id , Model model){
        System.out.println("deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"+id);
        Emp emp = empService.editEmp(id);
        model.addAttribute("emp",emp);
//        查询所有部门信息
        List<Emp> emps = empService.QueryAllEmp();
        model.addAttribute("depts",emps);
        return "emp/update";
    }

    @PostMapping(value="/put")
    public String editEmp(Emp emp){
        System.out.println(emp);
        int count = empService.updateEmp(emp);
        System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"+count);
        return "redirect:/emps";
    }
    //    删除
    @RequestMapping (value = "/emp2/{id}")
    public String deleteEmp(@PathVariable("id") Integer id){
        int count = empService.deleteEmp(id);
        return "redirect:/emps";
    }
    //注销
    @RequestMapping (value = "/emp/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }


//    分页
    @RequestMapping("/emps2/{page}")
    public String ShowEmps2(@PathVariable(value="page")Integer page,Model model)
    {
        Page<Object> objects = PageHelper.startPage(page, 4);
        int pageSize = objects.getPageSize();
        model.addAttribute("total",pageSize);
        System.out.println(pageSize);
        List<Emp> emps = empService.QueryAllEmp();
        model.addAttribute("emps",emps);
        return "emp/list";

    }
}
